import Vue from 'vue'
import { editUser, login, userCheck } from '../api/user'
export default {
    namespaced: true, //必写
    state: () => {
        return {
            tradelist: [
                { "name": "全部订单", "img": "https://m.taoshouyou.com/img/percenter/bug-4.png", "router": "/buy", "to": "all" },
                { "name": "待付款", "img": "https://m.taoshouyou.com/img/percenter/bug-1.png", "router": "/buy", "to": "fu" },
                { "name": "待发货", "img": "https://m.taoshouyou.com/img/percenter/bug-2.png", "router": "/buy", "to": "fa" },
                { "name": "待收货", "img": "https://m.taoshouyou.com/img/percenter/bug-3.png", "router": "/buy", "to": "shou" },
                { "name": "我的保单", "img": "https://m.taoshouyou.com/img/user/baodan.png", "router": "/buy", "to": "baodan" },
                { "name": "租号订单", "img": "https://m.taoshouyou.com/img/user/zuhao.png", "router": "/buy", "to": "dindan" },
                { "name": "代练订单", "img": "https://m.taoshouyou.com/img/user/dl.png", "router": "/buy", "to": "dailian" },
            ],
            selllist: [
                { "name": "全部商品", "img": "https://m.taoshouyou.com/img/percenter/allTrade.png", "router": "/percensell", "to": "all" },
                { "name": "待审核", "img": "https://m.taoshouyou.com/img/percenter/stayTrial.png", "router": "/percensell", "to": "fu" },
                { "name": "正出售", "img": "https://m.taoshouyou.com/img/percenter/beingSell.png", "router": "/percensell", "to": "fa" },
                { "name": "已出售", "img": "https://m.taoshouyou.com/img/percenter/sellOk.png", "router": "/percensell", "to": "shou" },
                { "name": "待发货", "img": "https://m.taoshouyou.com/img/percenter/stayHair.png", "router": "/percensell", "to": "baodan" },
                { "name": "待收货", "img": "https://m.taoshouyou.com/img/percenter/stayTake.png", "router": "/percensell", "to": "dindan" },
            ],
            orderlist: [],
            alllist: [],
            user: [
                { "name": "111", "money": 10000 }
            ],
            fabu: [],
            is: false,
            i: 0
        }
    },
    mutations: {
        delone(state, index) {
            state.orderlist.splice(index, 1)
        },
        add(state, index) {
            state.alllist.push(state.orderlist[index])
            state.user.money -= parseFloat(state.orderlist[index].price)

        },
        handadd(state, val) {

            if (val.code == 1) {
                state.is = true
                state.user = val
            } else if (val.code == 4 || val.token == -1) {
                state.is = false
                state.user.msg = val.msg
                state.user.code = val.code
            }
        },
        gai(state, bool) {
            state.is = false
        },
        jia(state, val) {
            Vue.set(val, 'money', 0)
            state.user.push(val)
        },
        insert(state, good) {
            state.orderlist.push(good)
        },
        addmoney(state, add) {
            state.user.money += parseInt(add.money)
        },

    },
    actions: {
        jiaAction(context, data) {
            editUser(data).then(val => {
                if (val.data.code == 1) {
                }
            })
        },
        handaddAction(context, data) {
            login(data).then(val => {
                if (val.data.code == 1) {
                    let userInfo = val.data
                    Vue.set(userInfo, 'money', 0)
                    localStorage.setItem('userInfo', JSON.stringify(userInfo))
                    context.commit('handadd', userInfo)
                }
            })
        },
        settingAction(context) {
            let { token } = context.state.user
            userCheck(token).then(val => {
                if (val.data.code != 1) {
                    context.commit('handadd', val.data)
                }
            })
        }
    }
}
